package com.ns.scs.rsr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.rsr.bo.CategoryEntryBO;
import com.ns.scs.rsr.bo.CategoryPageBO;
import com.ns.scs.rsr.dto.CategoryPageDto;
import com.ns.scs.rsr.entry.CategoryEntry;
import com.ns.scs.rsr.service.ICategoryService;
import com.ns.scs.rsr.vo.CategoryExtraVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 资源类别相关操作
 *
 * @author lw
 * @since 2024-11-15
 */
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
@CrossOrigin
public class CategoryController {

    private final ICategoryService service;

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "资源类别模块", name = "增")
    public ResultOk add(@RequestBody CategoryEntryBO bo) {
        CategoryEntry entry = new CategoryEntry();
        BeanUtils.copyProperties(bo, entry);
        entry.setSort(service.count() + 1);
        return ResultOk.getByBoolean(service.save(entry));
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "资源类别模块", name = "改")
    public ResultOk update(@RequestBody CategoryEntryBO bo) {
        CategoryEntry entry = new CategoryEntry();
        BeanUtils.copyProperties(bo, entry);
        return ResultOk.getByBoolean(service.updateById(entry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "资源类别模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(service.removeById(id));
    }

    /**
     * 删 - 根据id列表
     *
     * @param bo id集合
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "资源类别模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(service.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "资源类别模块", name = "详情")
    public ResultOk get(Long id) {
        CategoryEntry entry = service.getById(id);
        CategoryExtraVO vo = new CategoryExtraVO();
        BeanUtils.copyProperties(entry, vo);
        return ResultOk.getSuccessData(vo);
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "资源类别模块", name = "查-集合")
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody CategoryPageBO bo) {
        CategoryPageDto dto = new CategoryPageDto();
        BeanUtils.copyProperties(bo, dto);
        Page<CategoryExtraVO> page = service.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查-集合
     *
     * @param id 类别
     */
    @GetMapping("options")
    @ReqLog(module = "资源类别模块", name = "下拉列表", authFlag = false)
    @RateLimiter(time = 5, count = 15, limitType = LimitType.IP)
    public ResultOk getOptions(Long id) {
        return ResultOk.getSuccessData(service.getOptions(id));
    }
}
